﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using SimpleGameEngine;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Stopwatch _timer = new Stopwatch();
        private double _lastTime;
        private long _frameCounter;
        private GameState _gameState;

        private double currentStamp;
        private double lastStamp;
        private double elapsedTime;
        //private double animTimer;

        AnimSprite sprite = new AnimSprite("graphics/running_dog.gif", 60);

        public Form1()
        {
            InitializeComponent();

            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);

            initialize();
        }

        private void initialize()
        {
            //Startup the game state
            _gameState = new GameState(ClientSize);
            _gameState.Initialize();

            //Initialise and start the timer
            _lastTime = 0.0;
            _timer.Reset();
            _timer.Start();

            currentStamp = getTime();
            lastStamp = currentStamp;
            elapsedTime = (currentStamp - lastStamp) / 1000.00;

            //sprite.Start(90);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //Work out how long since we were last here in seconds
            double gameTime = _timer.ElapsedMilliseconds / 1000.0;
            double elapsedTime = gameTime - _lastTime;
            _lastTime = gameTime;
            _frameCounter++;


            currentStamp = getTime();
            elapsedTime = (currentStamp - lastStamp) / 1000.00;
            lastStamp = currentStamp;


            //Perform any animation and updates
            _gameState.Update(elapsedTime);


            //Draw everything
            _gameState.Draw(e.Graphics);
            
            /*
            animTimer += 1000 * elapsedTime;  //convert to milliseconds
            if (animTimer > 90) // advance frame every 90ms
            {
                animTimer = 0;
                sprite.advanceFrame();
            }
            e.Graphics.DrawImage(sprite.Paint(e.Graphics), 0, 40);
            */

            //Force the next Paint()
            Invalidate();
        }

        private double getTime()
        {
            return Math.Round((DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds);
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            GameKeyboard.KeyUp(e.KeyCode);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            GameKeyboard.KeyDown(e.KeyCode);
        }

    }

    //sprite class slightly modified after a sourcecode by David Sosby
    public class AnimSprite
    {
        private int frame, interval, width, height;
        private string imgFile;
        private Image img;
        private Timer frameTimer;

        public AnimSprite(string imgFile, int height)
        {
            this.height = height;
            this.imgFile = imgFile;
            img = new Bitmap(imgFile);
            width = img.Width;
        }

        public void Start(int interval)
        {
            this.interval = interval;
            frameTimer = new Timer();
            frameTimer.Interval = interval;
            //frameTimer.Tick += new EventHandler(advanceFrame);
            //frameTimer.Start();
        }

        public void Stop()
        {
            frameTimer.Stop();
            frameTimer.Dispose();
        }

        public Bitmap Paint(Graphics e)
        {
            Bitmap temp;
            Graphics tempGraphics;

            temp = new Bitmap(width, height, e);
            tempGraphics = Graphics.FromImage(temp);

            tempGraphics.DrawImageUnscaled(img, 0, 0 - (height * frame));

            tempGraphics.Dispose();
            return (temp);
        }

        public void advanceFrame()
        {
            frame++;
            if (frame >= img.Height / height)
                frame = 0;
        }
    }
}
